﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Data;
using System.Data.SqlClient;

namespace EM.operation.expense
{
    public partial class _default : System.Web.UI.Page
    {

        FillCombo fc = new FillCombo();


        private bool hasFULLAccess = false;
        private bool hasBMAccess = false;
        private bool hasLIMITEDAccess = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(17, Authentication.RedirectionURLTypes.LoginPage);

            hasFULLAccess = ua.HasPageFunctionAuthentication(17, "FULL");
            hasBMAccess = ua.HasPageFunctionAuthentication(17, "BM");
            hasLIMITEDAccess = ua.HasPageFunctionAuthentication(17, "LIMITED");

            if (!IsPostBack)
            {

                fc.ExpenseType(ddlEXPENSETYPE, 0, true, Convert.ToInt32(Session["WORKGROUPID"]));
                fc.LookUpValue(ddlEXPENSEPAYMENTTYPE, "", true, "EXPENSE_PAYMENT_TYPE", Session["LANGCODE"].ToString());
                rdpSTART.DbSelectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                rdpEND.DbSelectedDate = rdpSTART.SelectedDate.AddMonths(1);
            }

        }
        private DataTable GetData()
        {
            SqlCommand cmd = new SqlCommand();
            string sSQL = "";
            sSQL += @"
                        SELECT A.*, LVL.LOOKUPNAME AS INSTALLMENT, C.CURRENCYCODE FROM 
                        (SELECT E.EXPENSEID, CONVERT(NVARCHAR, E.EXPENSEDATE,104) EXPENSEDATE, E.AMOUNT, E.INSTALLMENTCOUNT, L.LOOKUPNAME EXPENSEPAYMENTTYPE,E.DESCRIPTION, ET.EXPENSETYPENAME,
                        CASE WHEN L.LOOKUPVALUE = 'BT' THEN (SELECT BANKACCOUNTNAME FROM TBLBANKACCOUNT WHERE BANKACCOUNTID = E.ENTITYID)
                        WHEN L.LOOKUPVALUE = 'C' THEN 'Nakit'
                        ELSE (SELECT CREDITCARDNAME FROM TBLCREDITCARD WHERE CREDITCARDID = E.ENTITYID) END AS ENTITY, E.INSTALLMENTFLG, E.CURRENCYID
                        FROM TBLEXPENSE E, TBLEXPENSETYPE ET, TBLLOOKUPVALUE_L L
                        WHERE E.EXPENSETYPEID = ET.EXPENSETYPEID
                        AND E.EXPENSEPAYMENTTYPE = L.LOOKUPVALUE
                        AND L.LOOKUPTYPE = 'EXPENSE_PAYMENT_TYPE'
                        AND L.LANGCODE = 'TR' AND E.WORKGROUPID = " + Session["WORKGROUPID"];

            if (ddlEXPENSEPAYMENTTYPE.SelectedValue != "")
            {
                sSQL += " AND E.EXPENSEPAYMENTTYPE = @EXPENSEPAYMENTTYPE";
                cmd.Parameters.AddWithValue("@EXPENSEPAYMENTTYPE", ddlEXPENSEPAYMENTTYPE.SelectedValue);
            }
            if (ddlEntity.SelectedValue != "" && ddlEntity.SelectedValue != "0")
            {
                sSQL += " AND E.ENTITYID = @ENTITYID ";
                cmd.Parameters.AddWithValue("@ENTITYID", ddlEntity.SelectedValue);
            }
            if (ddlEXPENSETYPE.SelectedValue != "" && ddlEXPENSETYPE.SelectedValue != "0")
            {
                sSQL += " AND E.EXPENSETYPEID = @EXPENSETYPEID";
                cmd.Parameters.AddWithValue("@EXPENSETYPEID", ddlEXPENSETYPE.SelectedValue);
            }
            if (rdpSTART.SelectedDate > Convert.ToDateTime("01/01/2007"))
            {
                sSQL += " AND E.EXPENSEDATE >= @EXPENSESTARTDATE";
                cmd.Parameters.AddWithValue("@EXPENSESTARTDATE", rdpSTART.SelectedDate);
            }
            if (rdpEND.SelectedDate > Convert.ToDateTime("01/01/2007"))
            {
                sSQL += " AND E.EXPENSEDATE <= @EXPENSEENDDATE";
                cmd.Parameters.AddWithValue("@EXPENSEENDDATE", rdpEND.SelectedDate);
            }

            sSQL += @"   ) AS A LEFT OUTER JOIN TBLLOOKUPVALUE_L LVL ON A.INSTALLMENTFLG = LVL.LOOKUPVALUE AND LVL.LANGCODE = 'TR' AND LVL.LOOKUPTYPE = 'SYS_YES_NO' 
                        LEFT OUTER JOIN TBLCURRENCY C ON A.CURRENCYID = C.CURRENCYID ";

            sSQL += "    ORDER BY EXPENSEDATE DESC";

            cmd.CommandText = sSQL;

            DataTable dt = new DataTable();
            DbHelper dsrv = new DbHelper();

            dt = dsrv.GetDataTable(cmd, "TBL");
            return dt;
        }

        protected void rgMaster_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = GetData();
            rgMaster.DataSource = dt;

            rgMaster.MasterTableView.NoMasterRecordsText = "Kayıt bulunamadı.";
            rgMaster.StatusBarSettings.ReadyText = dt.Rows.Count + " adet kayıt bulundu.";
        }

        protected void rgMaster_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Pager)
            {
                Label lblRecordCount = new Label();
                e.Item.Controls[0].Controls.AddAt(0, lblRecordCount);
                lblRecordCount.Text = rgMaster.StatusBarSettings.ReadyText + " | sayfaya git : ";

                rgMaster.ShowStatusBar = rgMaster.PageCount <= 1 ? true : false;
            }
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                DataRowView row = e.Item.DataItem as DataRowView;

                double amount = Convert.ToDouble(row["AMOUNT"]);
                string currencyCode = row["CURRENCYCODE"].ToString();

                Conversion Oc = new Conversion();
                e.Item.Cells[rgMaster.Columns.FindByUniqueName("AMOUNT").OrderIndex].Text = Oc.CNumS2C(amount) + " " + currencyCode;
            }
        }

        protected override void RaisePostBackEvent(IPostBackEventHandler source, String eventArgument)
        {
            base.RaisePostBackEvent(source, eventArgument);
            switch (eventArgument)
            {
                case "Rebind":
                    rgMaster.Rebind();
                    break;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            rgMaster.Rebind();

        }

        protected void ddlEXPENSEPAYMENTTYPE_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlEXPENSEPAYMENTTYPE.SelectedValue == "CC")
            {
                fc.CreditCard(ddlEntity, 0, true, 0, Convert.ToInt32(Session["WORKGROUPID"]));
            }
            else if (ddlEXPENSEPAYMENTTYPE.SelectedValue == "BT")
            {
                fc.BankAccount(ddlEntity, 0, true, 0, Convert.ToInt32(Session["WORKGROUPID"]));
            }
        }
    }
}